package com.orcamento.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import com.orcamento.entity.enums.Month;
import com.orcamento.util.Money;

@Entity
@Table(name="Categoria")
public class Category {

	@Id
	@SequenceGenerator(name="category_id", sequenceName="category_id")
	@GeneratedValue(generator="category_id", strategy=GenerationType.AUTO)
	@Column(name="IDCategoria")
	private Long id;
	@Column(name="StNome", nullable=false)
	private String name;
	
	@ManyToOne(targetEntity=Account.class)
	@JoinColumn(name="IDConta", referencedColumnName="IDPatrimonio", nullable=false)
	private Account account;

	@OneToMany(mappedBy="category")
	private List<Event> events = new ArrayList<Event>();
	
	public Money getTotalGain(Month month, Integer year) {
		Money total = new Money();
		for(Event event : events) {
			total = total.plus( event.getTotalGain(month, year) );
		}
		return total;
	}
	
	public Money getTotalSpents(Month month, Integer year) {
		Money total = new Money();
		for(Event event : events) {
			total = total.plus( event.getTotalSpents(month, year) );
		}
		return total;
	}
	
	public Money getTotalCashSpents(Month month, Integer year) {
		Money total = new Money();
		for(Event event : events) {
			total = total.plus( event.getTotalCashSpents(month, year) );
		}
		return total;
	}

	public Money getTotalCreditCardSpents(Month month, Integer year) {
		Money total = new Money();
		for(Event event : events) {
			total = total.plus( event.getTotalCreditCardSpents(month, year) );
		}
		return total;
	}

	public Integer getEventsSize() {
		return events.size();
	}
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public List<Event> getEvents() {
		return events;
	}
	public void setEvents(List<Event> events) {
		this.events = events;
	}

	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}

}
